package main

import (
	"context"
	"fmt"

	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/consumer"
	"github.com/apache/rocketmq-client-go/v2/primitive"
)

func main() {
	// 创建消费者
	c, err := rocketmq.NewPushConsumer(
		consumer.WithNameServer([]string{"127.0.0.1:9876"}), // 替换成你的 NameServer 地址
		consumer.WithGroupName("TagGroup"),
	)
	if err != nil {
		fmt.Printf("create consumer error: %s\n", err)
		return
	}

	// 订阅主题并指定过滤 Tag
	err = c.Subscribe("TagTopic", consumer.MessageSelector{
		Type:       consumer.TAG,
		Expression: "TagA",
	}, func(ctx context.Context, msgs ...*primitive.MessageExt) (consumer.ConsumeResult, error) {
		for _, msg := range msgs {
			fmt.Printf("consume message: %s\n", msg.Body)
		}
		return consumer.ConsumeSuccess, nil
	})
	if err != nil {
		fmt.Printf("subscribe error: %s\n", err)
		return
	}

	// 启动消费者
	err = c.Start()
	if err != nil {
		fmt.Printf("start consumer error: %s\n", err)
		return
	}

	// 阻塞主线程，保持消费者运行
	select {}
}
